<?php
add_filter('languages', 'add_install_language');

function install_db() {
    global $aldb;

    # Table structure for table `al_attachments`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->attachments` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `post_id` mediumint(8) unsigned NOT NULL,
  `name` varchar(64) NOT NULL,
  `mime` varchar(32) NOT NULL,
  `size` int(10) unsigned NOT NULL,
  `width` smallint(5) unsigned NOT NULL,
  `height` smallint(5) unsigned NOT NULL,
  `extra` varchar(32) NOT NULL,
  `upload_time` int(10) unsigned NOT NULL,
  `is_thumbnail` tinyint(1) unsigned NOT NULL,
  `downloads` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_capabilities`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->capabilities` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `group_id` smallint(5) unsigned NOT NULL,
  `user_id` mediumint(8) unsigned NOT NULL,
  `key` varchar(50) NOT NULL,
  `value` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_comments`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->comments` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `post_id` int(10) unsigned NOT NULL,
  `author` varchar(50) character set utf8 NOT NULL,
  `author_email` varchar(100) character set utf8 NOT NULL,
  `author_url` varchar(200) character set utf8 NOT NULL,
  `author_ip` varchar(15) character set utf8 NOT NULL,
  `post_time` int(10) unsigned NOT NULL,
  `content` text character set utf8 NOT NULL,
  `author_agent` varchar(255) character set utf8 NOT NULL,
  `parent` int(10) unsigned NOT NULL,
  `type` varchar(10) character set utf8 NOT NULL,
  `status` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_customfields`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->customfields` (
  `id` smallint(5) unsigned NOT NULL auto_increment,
  `diagram_id` smallint(5) unsigned NOT NULL,
  `field_title` varchar(30) NOT NULL,
  `field_key` varchar(30) NOT NULL,
  `field_order` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    #  Table structure for table `al_customvalues`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->customvalues` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `post_id` int(10) unsigned NOT NULL,
  `meta_key` varchar(50) NOT NULL,
  `meta_value` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_diagram`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->diagram` (
  `id` smallint(5) unsigned NOT NULL auto_increment,
  `type` varchar(10) NOT NULL,
  `title` varchar(50) NOT NULL,
  `content` text NOT NULL,
  `parent` smallint(5) unsigned NOT NULL,
  `url` varchar(200) NOT NULL,
  `metavalue` text NOT NULL,
  `displayorder` smallint(5) unsigned NOT NULL,
  `status` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_groups`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->groups` (
  `id` smallint(5) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `is_anonymous` tinyint(1) NOT NULL,
  `description` text NOT NULL,
  `is_active` tinyint(1) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    $aldb->query("INSERT INTO `$aldb->groups` (`id`, `title`, `is_anonymous`, `description`, `is_active`) VALUES (1, 'Anonymous user', '1', '', 1) ;");

    # Table structure for table `al_options`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->options` (
  `id` smallint(5) unsigned NOT NULL auto_increment,
  `option_name` varchar(60) NOT NULL,
  `option_value` text NOT NULL,
  `autoload` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_post2category`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->post2category` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `post_id` int(10) unsigned NOT NULL,
  `category_id` smallint(5) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_posts`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->posts` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `poster_id` mediumint(8) unsigned NOT NULL,
  `post_title` varchar(100) NOT NULL,
  `post_content` mediumtext NOT NULL,
  `read_password` varchar(50) NOT NULL,
  `ping_status` tinyint(1) unsigned NOT NULL,
  `to_ping` text NOT NULL,
  `comment_status` tinyint(1) unsigned NOT NULL,
  `post_time` int(10) unsigned NOT NULL,
  `post_type` tinyint(1) unsigned NOT NULL,
  `post_slug` varchar(50) NOT NULL,
  `star_status` tinyint(1) unsigned NOT NULL default '0',
  `post_status` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_sessions`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->sessions` (
  `id` char(32) NOT NULL,
  `data` varchar(255) NOT NULL,
  `expire` int(10) unsigned NOT NULL,
  `referer` varchar(100) NOT NULL,
  `user_id` mediumint(8) unsigned NOT NULL,
  `created` int(10) unsigned NOT NULL,
  `updated` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;");

    # Table structure for table `al_usermeta`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->usermeta` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `user_id` mediumint(8) unsigned NOT NULL,
  `meta_key` varchar(255) NOT NULL,
  `meta_value` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    # Table structure for table `al_users`
    $aldb->query("CREATE TABLE IF NOT EXISTS `$aldb->users` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `username` varchar(30) NOT NULL,
  `password` char(32) NOT NULL,
  `is_superuser` tinyint(1) unsigned NOT NULL,
  `nickname` varchar(30) NOT NULL,
  `group_id` smallint(5) unsigned NOT NULL,
  `posts` mediumint(8) unsigned NOT NULL,
  `date_joined` int(10) unsigned NOT NULL,
  `last_login` int(10) unsigned NOT NULL,
  `email` varchar(100) NOT NULL,
  `url` varchar(100) NOT NULL,
  `birthday` date NOT NULL,
  `icq` varchar(15) NOT NULL,
  `aim` varchar(100) NOT NULL,
  `yim` varchar(100) NOT NULL,
  `jabber` varchar(100) NOT NULL,
  `tell` varchar(30) NOT NULL,
  `activation_key` char(32) NOT NULL,
  `status` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");

    $aldb->query("INSERT INTO `$aldb->options` (`id`, `option_name`, `option_value`, `autoload`) VALUES (1, 'site_title', '', 1),
        (2, 'site_description', '', 1),
        (3, 'site_url', '', 1),
        (4, 'home_url', '', 1),
        (5, 'site_email', '', 1),
        (6, 'default_language', '" . DEFAULT_LANGUAGE . "', 1),
        (7, 'space_size', '100M', 1),
        (8, 'database_size', '100M', 1),
        (9, 'timezone_offset', '0', 1),
        (10, 'time_format', 'Y-m-d', 0),
        (11, 'url_model', 'default', 1),
        (12, 'template', 'default', 0),
        (13, 'gmt_offset', '0', 0),
        (14, 'upload_max_filesize', '2M', 0),
        (15, 'use_balanceTags', '0', 0),
        (16, 'active_plugins', '', 0),
        (17, 'is_thumbnail', '0', 0),
        (18, 'thumbnail_status', '0', 0),
        (19, 'thumbnail_quality', '100', 0),
        (20, 'thumbnail_width', '400', 0),
        (21, 'thumbnail_height', '300', 0),
        (22, 'is_watermark', '0', 0),
        (23, 'watermark_position', 'middle_center', 0),
        (24, 'watermark_condition_width', '0', 0),
        (25, 'watermark_condition_height', '0', 0),
        (26, 'watermark_transparent', '65', 0),
        (27, 'editor_width', '100%', 0),
        (28, 'editor_height', '500', 0)
        ;");

}

function insert_super_user($username, $password, $email) {
    global $aldb;
    $password = md5($password);
    $date_joined = time();
    $aldb->query("INSERT INTO `$aldb->users` (`id`, `username`, `password`, `is_superuser`, `nickname`, `group_id`, `posts`, `date_joined`, `last_login`, `email`, `url`, `birthday`, `icq`, `aim`, `yim`, `jabber`, `tell`, `activation_key`, `status`) VALUES (1, '$username', '$password', '1', '', '0', '0', '$date_joined', '0', '$email', '', '0000-00-00', '', '', '', '', '', '', '1')");
}

function mk_arlicle_dir() {
    // Creat attach folder
    al_mkdir(ROOT . 'www/attach');
    // crate backup folder
    al_mkdir(ROOT . 'www/backup');
    // create tmp folder
    al_mkdir(ROOT . 'www/tmp');
    al_mkdir(ROOT . 'www/tmp/cache');
    al_mkdir(ROOT . 'www/tmp/sessions');
    al_mkdir(ROOT . 'www/tmp/plugins');
    al_mkdir(ROOT . 'www/tmp/themes');

    # Write the urls
    $handle = fopen(ROOT . 'www/tmp/themes/urls.php', 'w');
    fwrite($handle, '');
    fclose($handle);

    // create plugins folder
    al_mkdir(ROOT . 'www/plugins');

}

function initial_data() {
    global $aldb;
    require_once(ARLICLE_WWW . 'themes/default/libs/initial_data.php');
    al_cache_urls();
}

function add_install_language($__language) {
    if (is_readable(ROOT . 'www/languages/' . DEFAULT_LANGUAGE . '_install.php')) {
        require_once ROOT . 'www/languages/' . DEFAULT_LANGUAGE . '_install.php';
    }
    return $__language;
}
?>
